package com.ts.crawler.pipeline;

import com.alibaba.fastjson.JSONArray;
import org.apache.commons.codec.digest.DigestUtils;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.utils.FilePersistentBase;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Map;

public class FlightInfoPipeline extends FilePersistentBase implements Pipeline {

    private final String path;
    public FlightInfoPipeline(String path){
        this.path = path;
    }

    public void process(ResultItems resultItems, Task task) {

        try{
            PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(this.getFile(path + DigestUtils.md5Hex(resultItems.getRequest().getUrl()) + ".json")), "UTF-8"));
            printWriter.println("{");
            int remain = resultItems.getAll().size();//记录剩余条目数
//            System.out.println(size);

            printWriter.print("\"result\":");
            JSONArray jsonArray = new JSONArray();
            for (Map.Entry<String, Object> entry : resultItems.getAll().entrySet()) {
                Map<String,String> map = (Map<String, String>) entry.getValue();
                jsonArray.add(map);
                System.out.println(jsonArray);
//                if (entry.getValue() instanceof Map) {//key:result1 ,value:map
////                    Iterable value = (Iterable) entry.getValue();
////                    printWriter.println(entry.getKey() + ":");
////
////                    for (Object o : value) {
////                        printWriter.println(o);
////                    }
//                    Map<String,String> map = (Map<String,String>)entry.getValue();
//
//                    sb.append("{");
//                    for(String key : map.keySet()){
//                        if(map.get(key) == null){
//                            continue;
//                        }
//                        sb.append("\"" + key + "\":\t\"" + map.get(key) + "\",");
//                    }
//                    sb.deleteCharAt(sb.lastIndexOf(","));
//                } else {
//                    sb.append("{\"").append(entry.getKey()).append("\":\t\"").append(entry.getValue()).append("\"}");
//                }
//                remain--;
//                if(remain == 0){
//                    sb.append("}");
//                }else{
//                    sb.append("},");
//                }
//                printWriter.println(sb);

            }

            printWriter.println(jsonArray);
            printWriter.println("}");
            printWriter.close();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}
